import instance from '@/api/axios'
// @ts-ignore
import {Tokens} from '@/stores/token'
// 封装的axios
// const ticketStore = useTicketStore()

interface responseInter {
    // data: any;
    success: boolean
    info: string
    result: any
    code: string
}

const request = async ({method, url, data, config,
//接口地址上有没有api
}: any): Promise<responseInter> => {
    method = method.toLowerCase()
    const  tokens=Tokens()
    //实时获取新的token
    instance.defaults.headers['Authorization'] = tokens.getToken()

    if (method == 'post') {
        return instance.post(url, data, { ...config },)
    } else if (method == 'get') {
        return instance.get(url, {
            params: data,
            ...config
        })
    } else if (method == 'delete') {
        return instance.delete(url, {
            params: data,
            ...config
        })
    } else if (method == 'put') {
        return instance.put(url, data, { ...config })
    } else {
        console.error('未知的method' + method)
        return {
            success: false,
            info: '未知method',
            result: {},
            code: 'fail',
        }
    }
}

export {
    request,
    type responseInter,
}
